<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "{{%wm_user}}".
 *
 * @property integer $id
 * @property string $nickname
 * @property string $username
 * @property string $password
 * @property integer $status
 * @property string $package_name
 * @property string $channels_name
 * @property integer $is_admin
 * @property integer $created_at
 * @property integer $updated_at
 */
class WmUser extends \yii\db\ActiveRecord
{
    public static $statuses = ['正常','禁用'];


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ]
                //'value' => function() { return date('U'); // unix timestamp },
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%wm_user}}';
    }

    public function relations()
    {
        return array(
            'products' => array(self::HAS_MANY, 'WmProduct', 'uid'),
        );

    }

    public function getProductCount()
    {
        // 关联的依据是 Order.customer_id = Customer.id
        return $this->hasMany(WmProduct::className(), ['uid' => 'id'])->count();
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username'], 'required'],
            [['status', 'is_admin', 'created_at', 'updated_at'], 'integer'],
            [['nickname'], 'string', 'max' => 40],
            [['username'], 'string', 'max' => 20],
            [['password', 'package_name', 'channels_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'nickname' => Yii::t('app', 'Nickname'),
            'username' => Yii::t('app', 'Username'),
            'productCount' => Yii::t('app', 'Product Count'),
            'password' => Yii::t('app', 'Password'),
            'Remember Me' => Yii::t('app', 'Remember Me'),
            'status' => Yii::t('app', 'Status'),
            'package_name' => Yii::t('app', 'Package Name'),
            'channels_name' => Yii::t('app', 'Channels Name'),
            'is_admin' => Yii::t('app', 'Is Admin'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
